

/*用DBUtil来修改*/


import utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo06_DBUtil {
    public static void main(String[] args) {
        Connection connection=null;
        PreparedStatement statement=null;
        /*对于更新删除都没有结果集返回，只会返回受影响的行数*/

        try {
            //1.获取数据库连接
            connection= DBUtil.getConnection();
            //2.定义SQl语句(根据Id修改学号
            String sql="update student set sno=? where id=?";
            //3.定义SQL的预处理对象
            statement =connection.prepareStatement(sql);
            //4.用真实的数据替换占位符
            Scanner scanner=new Scanner(System.in);
            String inputSno=scanner.next();
            long inputId=scanner.nextLong();
            statement.setString(1,inputSno);
            statement.setLong(2,inputId);
            //5.执行SQL
            int row=statement.executeUpdate();
            //6.获取执行结果
            if(row==1){
                System.out.println("修改成功");
            }else{
                System.out.println("修改失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{//7.释放资源
            DBUtil.close(null,statement,connection);
        }

    }
}
